package com.demo.generics.exercise;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class EmployeeTest {
    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee("july", 2000, new MyDate("7", "20", "2000")));
        employees.add(new Employee("tomdayu", 100000, new MyDate("5", "10", "1998")));
        employees.add(new Employee("tomdayu", 9000, new MyDate("10", "20", "1980")));

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (!(o1 instanceof Employee && o2 instanceof Employee)) {
                    System.out.println("类型不正确");
                }
                int i = o1.getName().compareTo(o2.getName());
                if (i != 0) return i;
                //相同则比较年月日。这段代码最好写在MyDate中。
//                int yearDis = Integer.valueOf(o1.getBirthday().getYear()) - Integer.valueOf(o2.getBirthday().getYear());
//                if (yearDis != 0) {
//                    return yearDis;
//                }
//                int monthDis = Integer.valueOf(o1.getBirthday().getMonth()) - Integer.valueOf(o2.getBirthday().getMonth());
//                if (monthDis != 0) {
//                    return monthDis;
//                }
//                return Integer.valueOf(o1.getBirthday().getDay()) - Integer.valueOf(o2.getBirthday().getDay());
                return o1.getBirthday().compareTo(o2.getBirthday());
            }
        });

        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }
}

class Employee {
    private String name;

    private double sal;

    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate implements Comparable<MyDate>{
    private String month;

    private String day;

    private String year;

    public MyDate(String month, String day, String year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "month='" + month + '\'' +
                ", day='" + day + '\'' +
                ", year='" + year + '\'' +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {
        int yearDis = Integer.valueOf(year) - Integer.valueOf(o.getYear());
        if (yearDis != 0) {
            return yearDis;
        }
        int monthDis = Integer.valueOf(month) - Integer.valueOf(o.getMonth());
        if (monthDis != 0) {
            return monthDis;
        }
        return Integer.valueOf(day) - Integer.valueOf(o.getDay());
    }
}
